<template>
	<view>
		<view class="x-c">
			<image class="logo-bg" :src='this.commonImg() + "login_bg.jpg"' mode=""></image>
		</view>
		<view class="logo">
			<view class="img">
				<image :src='this.commonImg() + "logob.png"' mode="widthFix"></image>
			</view>
		</view>
		<view class="modal-content">


			<view class="box-btn">
				<button v-show='!app' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
					点击登录
				</button>

				<!-- 
				<button @tap='noBtn' style="background: #ccc;color: #333">
					暂不登录
				</button> -->
				
				<button v-show='app' open-type="launchApp" :app-parameter="dataStr" @tap="goBack">
					返回App
				</button>

			</view>


		</view>
	</view>
</template>

<script>
	var _self;
	export default {
		data() {
			return {
				editModal: false,
				showInfo: false,
				userInfo: {
					avatar: '',
					nickname: ''
				},
				name: '',
				passwd: '',
				code: '',
				isShow: true,
				pid: '',
				path: '',
				dataStr: null,
				app: false
			}
		},

		onLoad: function(options) {
			uni.hideHomeButton()
			console.log(options, 'options')
			//app标识
			if (options.action) {
				uni.setStorageSync("app", options.action)
			}


			if (options.path) {
				this.path = options.path
			}
			let that = this;

			// if(options.query.pid||uni.getStorageSync("share_pid")){
			// 	const scene = options.query.pid
			// 	that.pid=scene
			// 	uni.setStorageSync("share_pid", scene);
			// 	return false
			// }else{
			// 	const scene = options.scene
			// 	uni.setStorageSync("share_pid", scene);
			// }
		},
		methods: {
			goBack(e){
				console.log(e)
			},
			getPhoneNumber: function(e) {
				console.log(e)
				console.log(uni.getStorageSync("share_pid"), '登录')
				let that = this
				this.isShow = false
				var commonUrl = this.websiteUrl()
				uni.showLoading({
					title: '登录中'
				})

				// #ifdef MP-WEIXIN	
				uni.login({
					success: function(res) {
						console.log(res)
						var code = res.code;
						if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
							uni.showToast({
								title: '取消授权',
								icon: "error"
							});
						} else {
							var encryptedData = e.detail.encryptedData;
							var iv = e.detail.iv;
							let url = ''

							// // #ifdef MP-TOUTIAO 
							// url = commonUrl + "dy-login"
							// // #endif


							url = commonUrl + "login"


							uni.request({
								url: url,
								data: {
									iv: iv,
									encrypt_data: encryptedData,
									code: code,
									bind_code: e.detail.code,
									pid: uni.getStorageSync("share_pid"),
								},
								method: 'POST',
								success: res => {
									if (res.data.code == 1) {
										console.log("login: " + res.data.data.token);
										uni.setStorageSync("token", res.data.data.token)
										that.dataStr = JSON.stringify({
											data: uni.getStorageSync("token"),
										})
											
										uni.showToast({
											title: "授权成功",
											icon: "success", //success 
											duration: 3500, //打开的时间
										});

										that.app = true
									} else {
										uni.showToast({
											title: "授权失败",
											icon: "error", //success 
											duration: 3500, //打开的时间
										});
										that.app = false
									}
								}
							})



						}
					},
				});
				// #endif
				uni.hideLoading();
			},

			noBtn() {
				if (this.path == '/pages/tabBar/home/home' || this.path == '/pages/tabBar/user/user' || this.path ==
					'/pages/tabBar/order/add') {

					uni.switchTab({
						url: this.path ? this.path : '/pages/tabBar/home/home'
					})
				} else {

					if (this.path) {
						uni.navigateBack({
							delta: 1 // delta值为1时表示返回的页面层数
						});
					} else {
						uni.switchTab({
							url: '/pages/tabBar/home/home'
						})
					}

					// uni.navigateTo({
					// 	url: this.path ? this.path : '/pages/tabBar/home/home'
					// })

				}
			},
			toPage() {
				uni.navigateTo({
					url: '/pages/user/yszc/yszc'
				});
			},
		}
	}
</script>

<style lang="scss">
	@import "../../static/css/login.scss";

	/* ---弹窗css--- */
	.modal-mask {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: #000;
		opacity: 0.5;
		overflow: hidden;
		color: #fff;
		z-index: 999;
	}

	.logo-bg {
		margin-top: 20px;
	}

	.modal-dialog {
		width: 72%;
		position: absolute;
		top: 30%;
		left: 14%;
		background: #fff;
		border-radius: 12rpx;
		z-index: 999;
	}

	.modal-content {
		width: 100%;
		text-align: center;
		position: fixed;
		bottom: 0;
	}

	.box-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 40rpx;
	}


	.iconWx {
		width: 52rpx;
		height: 41rpx;
		padding-right: 20rpx;
	}

	.iconPhone {
		width: 56rpx;
		height: 56rpx;
		padding-right: 15rpx;
	}

	.modal-content button {
		width: 80%;
		height: 80rpx;
		border-radius: 60rpx;
		font-size: 30rpx;
		color: #fff;
		background: #3cbdc5;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 0;
		box-sizing: border-box;
		margin: 0 20rpx;
	}

	.modal-content button::after {
		border: none;
	}
</style>